#!/usr/bin/env python
# -*- coding:utf-8 -*-
from datetime import datetime

from excel.PruTuutDb import PruTuutDb

class SysDictObj:
    col_keys = ["key", "key_eng", "col", "val", "add_time"]
    col_def_val = ["", "", "", "", None]

    id: 0
    key: ""
    key_eng: ""
    col: ""
    val: ""
    add_time: ""
    conn = None

    def __init__(self,conn):
        SysDictObj.conn = conn
        return

    @staticmethod
    def init_dict():
        g_param = {}
        g_param["id"] = 0
        g_param["key"] = ""
        g_param["key_eng"] = ""
        g_param["col"] = ""
        g_param["val"] = ""
        g_param["add_time"] = ""
        return g_param


    @staticmethod
    def split_params(params):
        g_param = []
        if params['key'] is not None and len(params['key']) > 0:
            g_param.append('`key`=' + "\'" + params['key'] + "\'")
        if params['key_eng'] is not None and len(params['key_eng']) > 0:
            g_param.append('`key_eng`=' + "\'" + params['key_eng'] + "\'")
        if params['col'] is not None and len(params['col']) > 0:
            g_param.append('`col`=' + "\'" + params['col'] + "\'")
        if params['val'] is not None and len(params['val']) > 0:
            g_param.append('`val`=' + "\'" + params['val'] + "\'")
        if params['add_time'] is not None and len(params['add_time']) > 0:
            g_param.append('`add_time`=' + "\'" + params['add_time'] + "\'")
        return ', '.join(str(i) for i in g_param)

    @staticmethod
    def concat_datas(g_param):
        # 获取当前时间
        current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        arry = [g_param["key"], g_param["key_eng"], g_param["col"], g_param["val"], g_param["add_time"]]
        _res = []
        for _val in arry:
            if isinstance(_val, str):
                _res.append(f"\'{_val}\'")
            else:
                if _val is None:
                    _res.append("null")
                else:
                    _res.append(_val)
        return ', '.join(str(i) for i in _res)

    @staticmethod
    def load_db_2_dict(data):
        g_param = {}
        g_param["id"] = data[0]
        g_param["key"] = data[1]
        g_param["key_eng"] = data[2]
        g_param["col"] = data[3]
        g_param["val"] = str(data[4])
        g_param["add_time"] = str(data[5])
        return g_param

    @staticmethod
    def to_string(with_flg):
        g_param = []
        if with_flg:
            g_param.append("`sys_dict`.`key`")
            g_param.append("`sys_dict`.`key_eng`")
            g_param.append("`sys_dict`.`col`")
            g_param.append("`sys_dict`.`val`")
            g_param.append("`sys_dict`.`add_time`")
        else:
            g_param.append("`key`")
            g_param.append("`key_eng`")
            g_param.append("`col`")
            g_param.append("`val`")
            g_param.append("`add_time`")
        return ', '.join(str(i) for i in g_param)

    def load_dict_val(self, key_eng):
        if key_eng is None or len(key_eng) == 0:
            return {"code": 404, "data": ""}
        fetch_sql = f" SELECT `id`,`key`,`key_eng`, `col`, `val`, `add_time` " \
                    f" FROM `sys_dict` WHERE 1=1 " \
                    f" AND `key_eng` = \'{key_eng}\'"
        try:
            items = self.conn.sql_fetch(fetch_sql)
            if len(items) > 0:
                res = [SysDictObj.load_db_2_dict(item) for item in items]
                return {"code": 200, "data": res[0]}
        except Exception as e:
            print(e)
        return {"code": 500, "data": None}

    def set_dict_val(self, key_eng,val):
        if key_eng is None or len(key_eng) == 0:
            return {"code": 404, "data": ""}
        update_sql = f" UPDATE `sys_dict` SET `val`= \'%s\' WHERE `key_eng`= \'%s\'" % (val, key_eng)
        try:
            self.conn.sql_commit(update_sql)
            return {"code": 200, "data": None}
        except Exception as e:
            print(e)
        return {"code": 500, "data": None}
